<style>
    .kami-table {
        width: 100%;
        border-collapse: collapse;
        margin: 15px 0;
        font-size: 14px;
    }
    .kami-table th {
        background-color: #F5F5F5;
        padding: 10px 15px;
        text-align: left;
        border: 1px solid #DDD;
        font-weight: bold;
    }
    .kami-table td {
        padding: 8px 15px;
        border: 1px solid #DDD;
        vertical-align: middle;
    }
    .kami-table tr:nth-child(even) {
        background-color: #F9F9F9;
    }
    .kami-table tr:hover {
        background-color: #F0F7FF;
    }
    .kami-table .status-active {
        color: #090;
    }
    .kami-table .status-inactive {
        color: #900;
    }
</style>
<!-- 一键导出当前列表$kami_list的卡密，可自选分隔符 -->
<div style="margin:10px 0;">
    <div style="display:inline-block;margin-right:10px;">
        <select id="delimiterSelect">
            <option value="
">换行(↵)</option>
            <option value=",">逗号(,)</option>
            <option value="|">竖线(|)</option>
            <option value=";">分号(;)</option>
        </select>
        <button onclick="exportWithDelimiter()" style="margin-right:10px;">导出卡密</button>
    </div>
</div>

<!-- 搜索表单 -->
<form name="cpform" method="post" autocomplete="off"  action="{$url_page}" method="get">
    <input type="text" name="id" value="{$query['id']}" placeholder="请输入ID">
    <input type="text" name="uid" value="{$query['uid']}" placeholder="请输入用户ID">
    <select name="status">
        <option value="">全部状态</option>
        <option value="1" {if $query['status'] == 1}selected{/if}>未使用</option>
        <option value="2" {if $query['status'] == 2}selected{/if}>已使用</option>
    </select>
    <input type="text" name="kami_key" value="{$query['kami_key']}" placeholder="请输入卡密">
    <select name="kami_type">
        <option value="">全部积分类型</option>
        <!--{loop $kami_type_list $kami_type}-->
            <option value="{$kami_type['id']}" <!--{if $query['kami_type'] == $kami_type['id']}-->selected<!--{/if}-->>{$kami_type['name']}</option>
        <!--{/loop}-->
    </select>
    最后有效时间：<input type="date" name="expire_time" value="{$expire_date}">
    <select name="pagesize">
        <option value="10" {if $pagesize == 10}selected{/if}>10条/页</option>
        <option value="20" {if $pagesize == 20}selected{/if}>20条/页</option>
        <option value="50" {if $pagesize == 50}selected{/if}>50条/页</option>
        <option value="100" {if $pagesize == 100}selected{/if}>100条/页</option>
    </select>
    <!-- <input type="text" name="page" value="{$page}"> -->
    <input type="submit" value="搜索">
</form>
<!-- 列表 -->
<table class="kami-table">
    <tr>
        <th width="5%">ID</th>
        <th width="5%">用户ID</th>
        <th width="5%">状态</th>
        <th width="10%">卡密</th>
        <th width="10%">分类</th>
        <th width="10%">最后有效时间</th>
        <th width="10%">操作</th>
    </tr>
    <!--{loop $kami_list $kami}-->
    <tr>
        <td>{$kami['id']}</td>
        <td>{$kami['uid']}</td>
        <td class="status-{$kami['status']}">{if $kami['status']==2}已购买{else}未购买{/if}</td>
        <td style="word-break: break-all;">{$kami['kami_key']}</td>
        <td>{$kami_type['kami_type']}
            <!--{loop $kami_type_list $kami_type}-->
                <!--{if $kami_type['id'] == $kami['kami_type']}-->
                    {$kami_type['name']}
                <!--{/if}-->
            <!--{/loop}-->
        </td>
        <td>
            <!--{if $kami['expire_time'] == 0}-->
            永久
            <!--{else}-->
            {eval echo date('Y-m-d H:i:s', $kami['expire_time']);}
            <!--{/if}-->
        </td>
        <td>
            <a href="{$url_page}&action_kami_buy=delete&action_kami_buy_id={$kami['id']}" onclick="return confirm('确定删除吗？');">删除</a>
        </td>
    </tr>
    <!--{/loop}-->
</table>
<!-- 分页 -->
{$multipage}

<script>
function exportWithDelimiter() {
    // 获取选择的分隔符
    var delimiter = document.getElementById('delimiterSelect').value;
    
    // 获取所有卡密
    var kamis = [];
    var rows = document.querySelectorAll('.kami-table tr:not(:first-child)');
    rows.forEach(function(row) {
        var cells = row.querySelectorAll('td');
        if(cells.length >= 4) {
            kamis.push(cells[3].textContent.trim()); // 卡密在第4列
        }
    });
    
    if(kamis.length === 0) {
        alert('没有可导出的卡密');
        return;
    }
    
    // 创建内容，使用选择的分隔符
    var content = kamis.join(delimiter === '\n' ? '\r\n' : delimiter);
    
    // 创建下载方式
    var file = new File([content], '卡密列表_' + new Date().toLocaleDateString() + '.txt', {
        type: 'text/plain;charset=utf-8'
    });
    
    // 创建下载链接
    var url = URL.createObjectURL(file);
    try {
        var a = document.createElement('a');
        a.href = url;
        a.download = file.name;
        document.body.appendChild(a);
        a.click();
    } finally {
        setTimeout(function() {
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        }, 100);
    }
}
</script>

